#include "envcard.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMouseEvent>

EnvCard::EnvCard(const QString &title, const QString &unit, QWidget *parent)
    : QFrame(parent)
{
    setStyleSheet("EnvCard {"
                 "  background-color: #f5fbf5;"
                 "  border: 1px solid #c8e6c9;"
                 "  border-radius: 8px;"
                 "  padding: 15px;"
                 "}");
    setupUI();
    titleLabel->setText(title);
    unitLabel->setText(unit);
}

void EnvCard::setupUI()
{
    // 使用垂直布局
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->setContentsMargins(10, 15, 10, 15); // 增加上下内边距
        mainLayout->setSpacing(5); // 减少间距

        // 标题
        titleLabel = new QLabel(this);
        titleLabel->setStyleSheet("font-size: 16px; font-weight: bold; color: #2c6e49;");
        titleLabel->setAlignment(Qt::AlignCenter);
        mainLayout->addWidget(titleLabel);

        // ========== 关键修改：添加间距 ==========
        // 添加垂直间距
        mainLayout->addSpacing(15); // 增加标题和数值之间的间距

        // 数值和单位容器
        QWidget *valueUnitContainer = new QWidget(this);
        QHBoxLayout *containerLayout = new QHBoxLayout(valueUnitContainer);
        containerLayout->setContentsMargins(0, 0, 0, 0);
        containerLayout->setSpacing(15); // 增加数值和单位之间的间距

        // 数值标签 - 左侧显示
        valueLabel = new QLabel("0.0", valueUnitContainer);
        valueLabel->setStyleSheet("font-size: 28px; font-weight: bold; color: #4b8a29;");
        valueLabel->setAlignment(Qt::AlignRight); // 居中显示
        valueLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        valueLabel->setMinimumWidth(80); // 设置最小宽度
        valueLabel->setTextInteractionFlags(Qt::NoTextInteraction); // 禁止文本交互
        valueLabel->setTextFormat(Qt::PlainText);
        valueLabel->setWordWrap(false);
       // valueLabel->setElideMode(Qt::ElideRight); // 如果空间不足，在右侧显示省略号
        containerLayout->addWidget(valueLabel);

        // 单位标签 - 右侧显示
        unitLabel = new QLabel("ppm", valueUnitContainer);
        unitLabel->setStyleSheet("font-size: 18px; color: #757575;");
        unitLabel->setAlignment(Qt::AlignCenter); // 居中显示
        unitLabel->setFixedWidth(50); // 固定宽度
        unitLabel->setTextInteractionFlags(Qt::NoTextInteraction); // 禁止文本交互
        containerLayout->addWidget(unitLabel);

        mainLayout->addWidget(valueUnitContainer);
        mainLayout->addStretch();
}

void EnvCard::setValue(double value)
{
    QString text = QString::number(value); // 'f' 表示定点数格式

    // 设置数值标签的文本
    valueLabel->setText(text);
    //valueLabel->setAlignment(Qt::AlignCenter);
   // valueLabel->setToolTip(text);
}

void EnvCard::setValueText(const QString &text)
{
    valueLabel->setText(text);
}

void EnvCard::setColor(const QColor &color)
{
    QString style = QString("font-size: 36px; font-weight: bold; color: %1;").arg(color.name());
    valueLabel->setStyleSheet(style);
}

void EnvCard::setClickable(bool clickable)
{
    isClickable = clickable;
    setCursor(clickable ? Qt::PointingHandCursor : Qt::ArrowCursor);
}

void EnvCard::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && isClickable) {
        emit clicked();
    }
    QFrame::mousePressEvent(event);
}

void EnvCard::enterEvent(QEvent *event)
{
    if (isClickable) {
        setStyleSheet("EnvCard {"
                     "  background-color: #e8f5e9;"
                     "  border: 2px solid #4b8a29;"
                     "  border-radius: 8px;"
                     "  padding: 15px;"
                     "}");
    }
    QFrame::enterEvent(event);
}

void EnvCard::leaveEvent(QEvent *event)
{
    if (isClickable) {
        setStyleSheet("EnvCard {"
                     "  background-color: #f5fbf5;"
                     "  border: 1px solid #c8e6c9;"
                     "  border-radius: 8px;"
                     "  padding: 15px;"
                     "}");
    }
    QFrame::leaveEvent(event);
}
